package com.ruoyi.serverinfo.controller;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.serverinfo.entity.TshockServerInfo;
import com.ruoyi.serverinfo.service.TshockServerService;
import com.ruoyi.serverinfo.service.TshockFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/tshock/server")
@CrossOrigin("*")
@Anonymous
public class TshockServerController {

    @Autowired
    private TshockServerService serverService;
    
    @Autowired
    private TshockFileService fileService;

    /**
     * 创建新服务器
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> createServer(@RequestBody TshockServerInfo serverInfo) {
        Map<String, Object> result = new HashMap<>();
        try {
            int count = serverService.saveServer(serverInfo);
            if (count > 0) {
                result.put("success", true);
                result.put("message", "服务器创建成功");
                result.put("data", serverInfo.getUuid());
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", "服务器创建失败");
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "服务器创建失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    /**
     * 更新服务器信息
     */
    @PutMapping("/{uuid}")
    public ResponseEntity<Map<String, Object>> updateServer(
            @PathVariable String uuid, 
            @RequestBody TshockServerInfo serverInfo) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 确保UUID一致
            serverInfo.setUuid(uuid);
            int count = serverService.updateServer(serverInfo);
            if (count > 0) {
                result.put("success", true);
                result.put("message", "服务器更新成功");
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", "服务器不存在或未更新");
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(result);
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "服务器更新失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    /**
     * 删除服务器
     */
    @DeleteMapping("/{uuid}")
    public ResponseEntity<Map<String, Object>> deleteServer(@PathVariable String uuid) {
        Map<String, Object> result = new HashMap<>();
        try {
            int count = serverService.deleteServerByUuid(uuid);
            if (count > 0) {
                result.put("success", true);
                result.put("message", "服务器删除成功");
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", "服务器不存在");
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(result);
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "服务器删除失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    /**
     * 根据UUID查询服务器
     */
    @GetMapping("/{uuid}")
    public ResponseEntity<Map<String, Object>> getServerByUuid(@PathVariable String uuid) {
        Map<String, Object> result = new HashMap<>();
        try {
            TshockServerInfo serverInfo = serverService.getServerByUuid(uuid);
            if (serverInfo != null) {
                result.put("success", true);
                result.put("data", serverInfo);
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", "服务器不存在");
                return ResponseEntity.status(HttpStatus.NOT_FOUND).body(result);
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    /**
     * 根据openid查询服务器列表
     */
    @GetMapping("/user/{openid}")
    public ResponseEntity<Map<String, Object>> getServersByOpenid(@PathVariable String openid) {
        Map<String, Object> result = new HashMap<>();
        try {
            List<TshockServerInfo> servers = serverService.getServersByOpenid(openid);
            result.put("success", true);
            result.put("count", servers.size());
            result.put("data", servers);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    /**
     * 获取所有服务器
     */
    @GetMapping
    public ResponseEntity<Map<String, Object>> getAllServers() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<TshockServerInfo> servers = serverService.getAllServers();
            result.put("success", true);
            result.put("count", servers.size());
            result.put("data", servers);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "查询失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }
}
    